package com.StarStudio.DAO;

import com.ruoyi.common.core.Entity.PanicBuyingCoupons;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PanicBuyingCouponsDAO {

    List<PanicBuyingCoupons> findPanicBuyingCouponsList(PanicBuyingCoupons panicBuyingCoupons);

    @Select("select count(panic_buying_coupons_id) from panic_buying_coupons where del_flag = false")
    long findTotalRecord();

    @Select("select * from panic_buying_coupons where coupons_id = #{coupons_id}")
    PanicBuyingCoupons findByCoupons_id(long coupons_id);

    int updatePanicBuyingCoupons(PanicBuyingCoupons panicBuyingCoupons);

    @Select("select * from panic_buying_coupons")
    List<PanicBuyingCoupons> PanicBuyingCouponsList();

    @Insert("insert into panic_buying_coupons values(#{panic_buying_coupons_id},#{coupons_id},#{panic_buying_price},#{panic_buying_coupons_stock},#{panic_buying_start_time}," +
            "#{panic_buying_end_time},#{panic_buying_status},#{del_flag})")
    void AddPanicBuyingCoupons(PanicBuyingCoupons panicBuyingCoupons);

    @Update("update panic_buying_coupons set del_flag = true,panic_buying_coupons_stock = 0 where panic_buying_coupons_id = #{panic_buying_coupons_id}")
    void DeletePanicBuyingCoupons(long panic_buying_coupons_id);

    @Select("select * from panic_buying_coupons where panic_buying_status = #{status}")
    List<PanicBuyingCoupons> findByStatus(int status);

    @Select("select panic_buying_coupons_stock from panic_buying_coupons where panic_buying_coupons_id = #{Panic_buying_coupons_id}")
    int findPanic_buying_coupons_stockByPanic_buying_coupons_id(long Panic_buying_coupons_id);

    @Select("select coupons_id from panic_buying_coupons where panic_buying_coupons_id = #{panic_buying_coupons_id}")
    long findCouponsIdByPBCId(long panic_buying_coupons_id);

    @Select("select IFNULL(panic_buying_coupons_stock,0) from panic_buying_coupons where coupons_id = #{coupon_id} and del_flag = false")
    Integer findPBCouponsStockByCouponsId(long coupon_id);

    List<Long> getCouponsIdListFromPanicBuyingCoupons();

    @Update("update panic_buying_coupons set panic_buying_status = #{panic_buying_status} where panic_buying_coupons_id = #{panic_buying_coupons_id}")
    void updatePanicBuyingCouponsStatus(@Param("panic_buying_coupons_id") long panic_buying_coupons_id,@Param("panic_buying_status") int panic_buying_status);

    @Update("update panic_buying_coupons set panic_buying_coupons_stock = panic_buying_coupons_stock - 1 where coupons_id = #{coupons_id} and panic_buying_coupons_stock > 0")
    int reduceStock(PanicBuyingCoupons panicBuyingCoupons);

    @Select("select * from panic_buying_coupons where panic_buying_status = 2 and del_flag = false")
    List<PanicBuyingCoupons> PanicBuyingEndStatusCouponsList();
}
